import 'package:auto_route/annotations.dart';
import 'package:auto_route/auto_route.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:provider/provider.dart';
import 'package:sonora/bloc/mine/sj_mine_bloc.dart';
import 'package:sonora/bloc/mine/sj_mine_event.dart';
import 'package:sonora/bloc/mine/sj_mine_state.dart';
import 'package:sonora/config/sj_app_initial.dart';
import 'package:sonora/shared/lib/constants/sj_colors.dart';
import 'package:sonora/utils/sj_device_utils.dart';
import 'package:sonora/utils/sj_log_utils.dart';
import 'package:sonora/utils/sj_text_style.dart';
import 'package:sonora/views/appbar/sj_appbar.dart';
import 'package:sonora/views/dialog/sj_medal_upgrade_dailog.dart';
import 'package:sonora/views/sj_root_provider.dart';

@RoutePage()
class SjSettingPage extends StatefulWidget {

  const SjSettingPage({super.key});
  @override
  State<SjSettingPage> createState() => _SjSettingPageState();
}

class _SjSettingPageState extends State<SjSettingPage> {
  late final SjMineBloc mineBloc = getit<SjMineBloc>();

  _SjSettingPageState();

  @override
  void initState() {
    super.initState();
    mineBloc.add(const SjMineEvent.loadedUserInfo());
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      extendBodyBehindAppBar: true, // 使内容延伸到导航
      appBar: SjAppbar(
        title: const Text(
          'Setting',
          style: SjTextStyle.customFontBlack20,
        ),
        backgroundColor: Colors.transparent, // 设置导航栏背景为透明
        elevation: 0, // 去除导航栏阴影
        leading: IconButton(
          icon: Image.asset('assets/images/nav_back.png'), // 左侧按钮
          onPressed: () {
            // 处理返回逻辑
            Navigator.of(context).pop();
          },
        ),
      ),
      body: BlocProvider<SjMineBloc>.value(
        value: mineBloc,
        child: Container(
        padding: const EdgeInsets.symmetric(horizontal: 32),
        width: double.infinity,
        decoration: const BoxDecoration(
          image: DecorationImage(
            image: AssetImage('assets/images/post_bg.png'),
            fit: BoxFit.cover,
          ),
        ),
        child: SafeArea(
            child: Column(children: [
          const SizedBox(
            height: 44,
          ),
          Image.asset(
            'assets/images/logo.png',
            width: 80,
            height: 80,
          ),
          const SizedBox(
            height: 35,
          ),
          BlocBuilder<SjMineBloc, SjMineState>(builder: (bContext, state) {
            return Container(
              padding: const EdgeInsets.fromLTRB(20, 14, 20, 12),
              decoration: const BoxDecoration(
                color: SjColors.themeAColor,
                borderRadius: BorderRadius.all(Radius.circular(12.0)),
              ),
              child: InkWell(
                  onTap: () {
                    ClipboardData data =
                        ClipboardData(text: state.userInfoData?.userId ?? '');
                    Clipboard.setData(data);
                    EasyLoading.showSuccess('Copy Successfully');
                  },
                  child: Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: [
                        Text(
                          'id：${state.userInfoData?.userId ?? ''}',
                          style: SjTextStyle.customFontBlack16,
                        ),
                        Image.asset('assets/images/setting_copy_icon.png')
                      ])),
            );
          }),
          const SizedBox(
            height: 24,
          ),
          Container(
            padding: const EdgeInsets.fromLTRB(20, 14, 20, 12),
            decoration: const BoxDecoration(
              color: SjColors.themeAColor,
              borderRadius: BorderRadius.all(Radius.circular(12.0)),
            ),
            child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  const Text(
                    'Auto Translate',
                    style: SjTextStyle.customFontBlack16,
                  ),
                  BlocBuilder<SjMineBloc, SjMineState>(
                      builder: (bContext, state) {
                    return Switch(
                      value: state.userInfoData?.autoTranslate ?? false,
                      onChanged: (bool change) {
                        mineBloc.add(SjMineEvent.autoTranslate(autoTranslate: state.userInfoData?.autoTranslate ?? false));
                        Provider.of<SjRootProvider>(context, listen: false).changeIndex(0);
                      },
                      activeColor: SjColors.blackColor,
                      inactiveTrackColor: SjColors.themeColor,
                    );
                  }),
                ]),
          ),
          const SizedBox(
            height: 24,
          ),
          InkWell(
            child: Container(
              padding: const EdgeInsets.fromLTRB(20, 14, 20, 12),
              decoration: const BoxDecoration(
                color: SjColors.themeAColor,
                borderRadius: BorderRadius.all(Radius.circular(12.0)),
              ),
              child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: const [
                    Text(
                      'Logout',
                      style: SjTextStyle.customFontBlack16,
                    ),
                  ]),
            ),
            onTap: () {
              _showNotEnoughCoinDialog(
                  context, 'Are you sure you want to logout the account?', () {
                mineBloc.add(SjMineEventLogout(context: context));
                Provider.of<SjRootProvider>(context, listen: false).changeIndex(0);
              });
              SjLogPvManger.stat(page: SjLogPages.sjActionNameLogout);
            },
          ),
          const SizedBox(
            height: 24,
          ),
          InkWell(
            child: Container(
              padding: const EdgeInsets.fromLTRB(20, 14, 20, 12),
              decoration: const BoxDecoration(
                color: SjColors.themeAColor,
                borderRadius: BorderRadius.all(Radius.circular(12.0)),
              ),
              child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: const [
                    Text(
                      'Delete account',
                      style: SjTextStyle.customFontBlack16,
                    ),
                  ]),
            ),
            onTap: () {
              _showNotEnoughCoinDialog(
                  context, 'Are you sure you want to delete the account?', () {
                mineBloc.add(SjMineEventDeleteAccount(context: context));
                Provider.of<SjRootProvider>(context, listen: false).changeIndex(0);
              });
              SjLogPvManger.stat(page: SjLogPages.sjActionNameDeleteaccount);
            },
          )
        ])),
      ),
      )
    );
  }

  void _showNotEnoughCoinDialog(
      BuildContext context, String msg, Function callback) {
    showDialog(
        barrierDismissible: false,
        context: context,
        builder: (BuildContext context) {
          return SjMedalUpgradeDialog(
            content: msg,
            onAgree: () {
              callback.call();
            },
            onCancel: () {},
            cancel: 'Return',
            agree: 'Yes',
          );
        });
  }
}
